package cn.com.customer.manager.ui.order.detail

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.EmailAttachmentAdapter
import cn.com.customer.manager.adapter.GoodsAddAdapter
import cn.com.customer.manager.adapter.RemittanceAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.*
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.ui.order.addOrder.AddOrderActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.layout.ExpandableLayout
import kotlinx.android.synthetic.main.activity_order_detail.*
import kotlinx.android.synthetic.main.menu_order_fab.*

class OrderDetailActivity : BaseActivity(), OrderDetailContract.View, BaseQuickAdapter.OnItemClickListener {
    var mPresenter: OrderDetailContract.Presenter = OrderDetailPresenter(this);
    lateinit var mRemittanceAdapter: RemittanceAdapter
    lateinit var mGoodListAdapter: GoodsAddAdapter
    lateinit var mAttachmentAdapter: EmailAttachmentAdapter
    private var mAttachmentData: MutableList<Attachments> = mutableListOf()
    var params: MutableMap<String, String> = mutableMapOf()
    var goodsList:  MutableList<Goods> = mutableListOf()
    var orderId: String = ""
     var receivedPayments: MutableList<ReceivedPayment> = mutableListOf()
    override fun getLayoutId(): Int = R.layout.activity_order_detail


    override fun initView(savedInstanceState: Bundle?) {
        initActionBar(getString(R.string.order_detail))
        initListener()
        initRv()
        initData()
        getData()
        fab_menu.close(false)
    }

    private fun getData() {
        orderId = intent.getStringExtra("orderId")
        params["orderId"] = orderId
        mPresenter.getData(params)
        mPresenter.getGoodList(params)
        mPresenter.getReceivedPaymentsList(params)
    }

    private fun initRv() {
        WidgetUtils.initRecyclerView(od_remittance_rv)
        mRemittanceAdapter = RemittanceAdapter(R.layout.item_remittance_list)
        od_remittance_rv.adapter = mRemittanceAdapter
        od_attachments.layoutManager = GridLayoutManager(this, 5)
        mAttachmentAdapter = EmailAttachmentAdapter(R.layout.item_attachment_list)
        od_order_rv.adapter = mAttachmentAdapter
        mAttachmentAdapter.onItemClickListener = this

    }

    private fun initListener() {
        od_remittance_icon.setOnClickListener(this)
        od_order_icon.setOnClickListener(this)
        fab_modify.setOnClickListener(this)
        fab_add.setOnClickListener(this)
    }

    private fun initData() {
        od_remittance_exLayout.expand()
        od_order_exLayout.expand()
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v) {
            od_remittance_icon -> {
                changeExLayout(od_remittance_exLayout, od_remittance_icon)
            }
            od_order_icon -> {
                changeExLayout(od_order_exLayout, od_order_icon)
            }
            fab_add -> {
                showCustomDialog()
            }
            fab_modify ->{
                startActivity(Intent(this, AddOrderActivity::class.java).putExtra("orderId", orderId).putExtra("type",1))

            }
        }
    }

    /**
     * 添加回款记录
     */
    private fun showCustomDialog() {
        var dialog = MaterialDialog.Builder(this)
                .customView(R.layout.item_record_add_order, true)
                .title(R.string.add_order)
                .positiveText(R.string.confirm_submit)
                .negativeText(R.string.picture_cancel)
                .onPositive { dialog, which ->
                    var time = dialog.findViewById<EditText>(R.id.record_time_content).text.toString()
                    var price = dialog.findViewById<EditText>(R.id.record_price_content).text.toString()
                    var remark = dialog.findViewById<EditText>(R.id.record_remark).text.toString()
                    var params: MutableMap<String, String> = mutableMapOf()
                    params["orderId"] = orderId
                    params["receivedDate"] = time
                    params["receivedAmount"] = price
                    params["remark"] = remark
                    mPresenter.receivedPaymentAdd(params)
                    dialog.dismiss()
                }
                .onNegative { dialog, which ->
                    dialog.dismiss()
                }
                .show()
    }

    private fun changeExLayout(ex: ExpandableLayout, icon: ImageView) {
        if (ex.isExpanded) {
            ex.collapse()
            icon.setImageResource(R.mipmap.icon_arrow_up)
        } else {
            ex.expand()
            icon.setImageResource(R.mipmap.icon_arrow_down)
        }
    }

    override fun setData(data: OrderDetailInfo) {
        od_name.text = data.orderName
        od_num.text = getString(R.string.order_num) + data.orderSerialNumber
        od_count.text = getString(R.string.order_count) + data.orderTotalAmount
        od_time.text = getString(R.string.date_time) + data.orderDate
        od_payWay.text = getString(R.string.order_pay_way) + data.pay_way
        od_state.text = data.status
        od_customer_name.text = getString(R.string.customer_name) + data.name
        od_orderReceivedAmount.text = getString(R.string.order_ReceivedAmount) + data.orderReceivedAmount
        od_bank_name.text = getString(R.string.bank_name) + data.payBankName
        od_bank_account_name.text = getString(R.string.pay_bankAccountName) + data.payBankName
        od_bank_account.text = getString(R.string.pay_bankAccount) + data.payBankAccount
        od_bank_address.text = getString(R.string.payBankAddress) + data.payBankAddress
        od_bank_code.text = getString(R.string.payBankCode) + data.payBankCode
        data.attachments.forEach {
            mAttachmentData.add(Attachments(it.url))
        }
        mAttachmentAdapter.addData(mAttachmentData)
    }

    override fun setGoodList(data: GoodListInfo) {
        if (data.goods.isNotEmpty()) {
            goodsList= data.goods as MutableList<Goods>
            WidgetUtils.initRecyclerView(od_order_rv)
            mGoodListAdapter = GoodsAddAdapter(R.layout.item_order_goods)
            od_order_rv.adapter = mGoodListAdapter
            mGoodListAdapter.setNewData(goodsList)

        }
    }

    override fun setReceivedPaymentsList(data: ReceivePayListInfo) {
        if (!data.receivedPayments.isNullOrEmpty()) {
            receivedPayments= data.receivedPayments as MutableList<ReceivedPayment>
            mRemittanceAdapter.setNewData(receivedPayments)
        }

    }

    override fun addSuccess() {
        mPresenter.getReceivedPaymentsList(params)
    }

    override fun setPresenter(presenter: OrderDetailContract.Presenter) {
        mPresenter = presenter
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        BaseApi.downfile(mAttachmentData[position].url)
    }
}
